package net.net23.sysdent.client.ui;

import java.util.Map.Entry;
import java.util.Set;

import net.net23.sysdent.client.controller.AppController;
import net.net23.sysdent.client.ui.menu.MenuItem;
import net.net23.sysdent.client.ui.menu.MenuItemHandler;
import net.net23.sysdent.client.ui.widgets.links.LinksSearcher;
import net.net23.sysdent.client.ui.widgets.model.ContentWidget;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

public class T4LHorinzontalMenu extends Composite implements MenuItemHandler {

	private T4LLayout layoutManager;

	public T4LHorinzontalMenu() {
		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		vp.setHeight("20px");
		vp.setStyleName("t4l-horizontal-menu");
		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);

		AppController.registerWidget(new LinksSearcher("links",
				AppController.images.link()));
		// AppController.registerWidget(new TagsSearcher("tags",
		// AppController.images.tag()));

		FlexTable options = new FlexTable();
		FlexCellFormatter flexCellFormatter = options.getFlexCellFormatter();

		Set<Entry<String, ContentWidget>> entrySet = AppController
				.getRegisteredWidgets().entrySet();
		int column = 0;
		for (Entry<String, ContentWidget> entry : entrySet) {
			ContentWidget contentWidget = entry.getValue();
			MenuItem anchor = new MenuItem(contentWidget.getKey());
			anchor.addMenuItemHandler(this);
			flexCellFormatter.setWidth(0, column, "100px");
			flexCellFormatter.setHorizontalAlignment(0, column,
					HasHorizontalAlignment.ALIGN_CENTER);
			options.setWidget(0, column, anchor);
			column++;
		}

		vp.add(options);
		initWidget(vp);

	}

	public void setLayoutManager(T4LLayout t4lLayout) {
		this.layoutManager = t4lLayout;
	}

	@Override
	public void itemSelected(MenuItem source) {
		String itemKey = source.getKey();
		ContentWidget contentWidget = AppController.getRegisteredWidgets().get(
				itemKey);
		layoutManager.setContents(contentWidget);
		History.newItem(itemKey);
	}

}
